#interface.py

import wx
import os

import custom_events
import gvf_snakes
import snakeinterface
import PaintToolsWindow
import PaintWindow

class DoodleFrame(wx.Frame):
    def __init__(self, parent=None):
        super(DoodleFrame, self).__init__(parent, title="Python Image to PDB Designer v0.1",
            style=wx.DEFAULT_FRAME_STYLE|wx.NO_FULL_REPAINT_ON_RESIZE)
        self.dirname=''
        self.doodle = PaintWindow.DoodleWindow(self)
        self.CreateStatusBar() # A Statusbar in the bottom of the window

        # Setting up the menu.
        filemenu= wx.Menu()
        menuOpen = filemenu.Append(wx.ID_OPEN, "&Open"," Open a file to edit")
        menuAbout= filemenu.Append(wx.ID_ABOUT, "&About"," Information about this program")
        menuExit = filemenu.Append(wx.ID_EXIT,"E&xit"," Terminate the program")
        
        viewmenu = wx.Menu()
        menuGVF = viewmenu.Append(wx.ID_ANY, "&GVF", "Toggle GVF View", kind=wx.ITEM_CHECK)
        menuSnake = viewmenu.Append(wx.ID_ANY, "&Snake", "Toggle Snake View", kind=wx.ITEM_CHECK)
        menuGaussian = viewmenu.Append(wx.ID_ANY, "&Gaussian", "Toggle Smoothed Gaussian View", kind=wx.ITEM_CHECK)

        viewmenu.Check(menuGVF.GetId(), True)
        viewmenu.Check(menuSnake.GetId(), True)
        viewmenu.Check(menuGaussian.GetId(), True)
        # Creating the menubar.
        menuBar = wx.MenuBar()
        menuBar.Append(filemenu,"&File") # Adding the "filemenu" to the MenuBar
        menuBar.Append(viewmenu,"&View")
        self.SetMenuBar(menuBar)  # Adding the MenuBar to the Frame content.

        # Notebook Tabs
        self.notebook = wx.Notebook(self)
        self.drawingwindow = PaintToolsWindow.DrawingWindow(self.notebook, self.doodle)
        self.notebook.AddPage(self.drawingwindow, "Drawing Tools")
        #self.interface = snakeinterface.SnakeInterfaceWindow(self.notebook, self.doodle)
        #self.notebook.AddPage(self.interface, "Snake/GVF")
        
        self.doodle.SetSize(self.doodle.initsize)
        
        # Sizers
        Sizer = wx.BoxSizer(wx.HORIZONTAL)
        Sizer.Add(self.doodle, 1, wx.EXPAND|wx.SHAPED, 5)
        Sizer.Add(self.notebook, 0, wx.EXPAND, 5)
        
        # Events.
        self.Bind(wx.EVT_MENU, self.OnOpen, menuOpen)
        self.Bind(wx.EVT_MENU, self.OnExit, menuExit)
        self.Bind(wx.EVT_MENU, self.OnAbout, menuAbout)
        self.Bind(wx.EVT_MENU, self.OnViewGVF, menuGVF)
        self.Bind(wx.EVT_MENU, self.OnViewSnake, menuSnake)
        self.Bind(wx.EVT_MENU, self.OnViewGaussian, menuGaussian)
        
        self.sizer = Sizer
        self.SetSizerAndFit(Sizer)
        
        self.SetSize(wx.Size(800,600))
        
    def OnAbout(self,e):
        # Create a message dialog box
        dlg = wx.MessageDialog(self, " Alpha Version of ItP Designer\n For Academic Use only.\n\n"
                               +" Contact Dr. Emil Alexov (ealexov@clemson.edu)\n"
                               +" for information on commercial usage.", 
                               "Python Image to PDB Designer v0.1", wx.OK)
        dlg.ShowModal() # Shows it
        dlg.Destroy() # finally destroy it when finished.

    def OnExit(self,e):
        self.Close(True)  # Close the frame.

    def OnOpen(self,e):
        """ Open a file"""
        dlg = wx.FileDialog(self, "Choose a file", self.dirname, "", "*.*", wx.OPEN)
        if dlg.ShowModal() == wx.ID_OK:
            self.filename = dlg.GetFilename()
            self.dirname = dlg.GetDirectory()
            self.doodle.openImage(os.path.join(self.dirname, self.filename))
        dlg.Destroy()
        
    def OnViewGVF(self,e):
        self.doodle.viewGVF = not self.doodle.viewGVF
        self.doodle.reInitBuffer = True
    def OnViewSnake(self,e):
        self.doodle.viewSnake = not self.doodle.viewSnake
        self.doodle.reInitBuffer = True
    def OnViewGaussian(self,e):
        self.doodle.viewGaussian = not self.doodle.viewGaussian
        self.doodle.reInitBuffer = True
    
    #hack to get the sizer to update the image aspect ratio, requiredfor GVF field lines
    def imgUpdate (self, size):
        #print "newimg:", size
        self.doodle.SetSize(size)
        self.GetSizer().Replace(self.doodle,self.doodle)
        self.GetSizer().Layout()

if __name__ == '__main__':
    app = wx.App()
    frame = DoodleFrame()
    frame.Show()
    app.MainLoop()
